package com.titaniume.java1;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
 *@program: JavaSenior
 *@description: java内置的4大核心函数式接口
 *
 * 消费型 接口 Comsumer<T> void accept(T t)
 * 供给型接口 Supplier<T>  T get()
 * 函数式接口 Function<T,R> R apply(T t)
 * 断定型接口 Predicate<T> boolean test(T t)
 *
 *@author: renfei.huang
 *@create: 2021-11-02 15:30
 */
public class LambdTest2 {

    @Test
    public  void test1(){

        //以前
        happyTime(122, new Consumer<Double>() {
            @Override
            public void accept(Double aDouble) {
                System.out.println("我好累啊，按摩去 花费"+ aDouble);
            }
        });
        System.out.println("----------------------------------");
        //Lambda
        happyTime(100,(money)->  System.out.println("我好累啊，按摩去 花费"+ money));


    }

    public  void happyTime(double money, Consumer<Double> consumer){
        consumer.accept(money);
    }


    @Test
    public  void test2(){
        //以前
        List<String> list = Arrays.asList("北京","南京","天津","东京","普京");
        List<String> stringList = filterString(list, new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.contains("京");
            }
        });
        stringList.forEach(System.out::println);
        System.out.println("----------------------------------");
        //Lambda
        List<String> stringList1 = filterString(list, s -> {
            return s.contains("京");
        });
        stringList1.forEach(System.out::println);

    }


    //根据某种规则过滤集合中的字符串，此规则由Predicate方法决定
    public  List<String>  filterString(List<String> list, Predicate<String> predicate){
        List<String> filterList = new ArrayList<>();
        for (String s : list) {
            if(predicate.test(s)){
                filterList.add(s);
            }
        }
        return filterList;
    }

}
